<?php

require_once '_inc.php';

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$catid = intval($_POST['id']);
	$catename = $_POST['catename'];
	$py       = $_POST['py'];
	$seokey   = $_POST['seokeywords'];
	$seodes   = $_POST['seodescription'];
	if($catid) {
		$sql = "update ".$gDb['prefix']."category set catename='$catename', py='$py', seokeywords='$seokey', seodescription='$seodes' where id=$cateid";
		if($db->query($sql))
		{
			$backUrl = $_POST['backUrl'];
			if ( !empty( $backUrl ) )
			{
				header( "location: ".base64_decode( $backUrl ) );
				exit( );
			}
		}
	} else {
		$sql = "insert into ".$gDb['prefix']."category set catename='$catename', py='$py', seokeywords='$seokey',seodescription='$seodes'";
		if($db->query($sql))
		{
			$backUrl = $_POST['backUrl'];
			if ( !empty( $backUrl ) )
			{
				header( "location: ".base64_decode( $backUrl ) );
				exit( );
			}
		}
	}
	header( "location: news_cat.php" );
	exit( );
}


$op = $_GET['op'];
$backUrl = $_GET['back'];
switch($op)
{
	case 'add':
		$tpl->assign('backUrl',$backUrl);
		$tpl->display('cat_edit.tpl');
		break;
	case 'edit':
		$id = intval($_GET['id']);
		if($id) {
			$cat = $db->fetchrow("select * from ".$gDb['prefix']."category where id=$id");
			$tpl->assign('cat',$cat);
			$tpl->display('cat_edit.tpl');
		}
		else {
			echo '错误的ID';
		}
		break;
	case 'del':
		$id = intval($_GET['id']);
		if($id) {
			if($db->query("delete from ".$gDb['prefix']."category where id=$id"))
			{
				header( "location: news_cat.php" );
				exit( );
			}
		}
		break;
}